package lt.ntec.danblog.server.blog;

import java.util.List;

import lt.ntec.danblog.server.blog.model.Post;
import lt.ntec.danblog.server.core.SimpleMessage;
import lt.ntec.danblog.server.core.Message;
import lt.ntec.danblog.server.core.validator.AbstractValidator;
import lt.ntec.danblog.server.core.validator.Rule;
import lt.ntec.danblog.server.core.validator.Validator;
import lt.ntec.danblog.server.core.validator.ValidationException;
import lt.ntec.danblog.server.core.validator.rules.TagsRule;
import lt.ntec.danblog.server.core.validator.rules.TitleRule;

public class PostValidator extends AbstractValidator implements Validator {

    @Override
    public boolean isValid(Object object) {

        if (!(object instanceof Post)) {
            messages.add(new SimpleMessage(Message.Type.NOTICE, "Wrong type"));
            return false;
        }
        Post post = (Post) object;

        boolean isValid = true;

        Rule title = new TitleRule();
        Rule tags = new TagsRule();

        if (!title.isValid(post.getTitle())) {
            messages.add(title.getMessage());
            isValid = false;
        }

        if (!tags.isValid(post.getTagString())) {
            messages.add(tags.getMessage());
            isValid = false;
        }
        return true & isValid;
    }

    public static class PostValidationException extends ValidationException {

        /**
         *
         */
        private static final long serialVersionUID = -4112461958129044298L;

        public PostValidationException(List<Message> messages) {
            super(messages);
        }
    }
}
